package util;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gonfre.quientelo.R;
import com.gonfre.quientelo.RegisterPurchaserActivity;
import com.gonfre.quientelo.RequestDetailsActivity;
import com.gonfre.quientelo.RequestListActivity;

/**
 * Created by Gonfre on 09/03/2015.
 */
public class RequestListAdapter extends RecyclerView.Adapter<RequestListAdapter.ViewHolder> {
    private Response mDataset;
    private Context mContext;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public View priceView;
        public ViewHolder(View v) {
            super(v);
            priceView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public RequestListAdapter(Response myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public RequestListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.request_layout2, parent, false);
        // set the view's size, margins, paddings and layout parameters
        //...
        ViewHolder vh = new ViewHolder(v);

        mContext = parent.getContext();

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mContext, RequestDetailsActivity.class);

                //intent.putExtra("activityToBack", "Login");
                mContext.startActivity(intent);
            }
        });

        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        TextView tDesc = (TextView)holder.priceView.findViewById(R.id.textPrimary);
        TextView tPurc = (TextView)holder.priceView.findViewById(R.id.textSecondary);
        TextView tDate = (TextView)holder.priceView.findViewById(R.id.textThird);
        ImageView iPhoto = (ImageView)holder.priceView.findViewById(R.id.imgHasPhoto);

        tDesc.setText(mDataset.getBodyValueArrayStr("request_list", position, "description"));
        tPurc.setText(mDataset.getBodyValueArrayStr("request_list", position, "purchaser"));
        tDate.setText(Tools.getShortTimeFromDate(mDataset.getBodyValueArrayStr("request_list", position, "date"), mContext));
        if (mDataset.getBodyValueArrayStr("request_list", position, "with_photo").equals("S")) {
            iPhoto.setImageResource(R.drawable.ic_file_image_box_grey600_24dp);
        } else {
            iPhoto.setImageResource(R.drawable.none);
        }
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.getListLength("request_list");
    }
}